package com.yanqu.road.server.gameplayer.module.activity.jumppot;

import com.yanqu.road.dao.impl.player.UserCommonActivityDataDaoImpl;
import com.yanqu.road.entity.autodata.hotpotjump.UserHotpotJumpData;
import com.yanqu.road.entity.autodata.hotpotjump.UserHotpotJumpGameData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eShopType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.log.hotpotjump.*;
import com.yanqu.road.logic.activity.hotpotjump.HotpotJumpConfig;
import com.yanqu.road.logic.activity.hotpotjump.HotpotJumpEvaluateConfig;
import com.yanqu.road.logic.activity.hotpotjump.game.HotpotJumpGameLogic;
import com.yanqu.road.logic.pb.HotpotJumpPb;
import com.yanqu.road.pb.activity.HotpotJumpProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fund.ActivityFundModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.useract.UserActModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.jumppot.HotpotJumpMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class HotpotJumpModule extends GeneralModule {
    public HotpotJumpModule(GamePlayer player) {
        super(player);
    }

    private UserHotpotJumpData userData;
    private UserHotpotJumpGameData userGameData;
    private UserHotpotJumpGameData practiceGameData; //练习模式，不入库

    @Override
    public boolean loadData() {
        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        if (config == null) {
            return true;
        }
        userData = new UserCommonActivityDataDaoImpl<UserHotpotJumpData>().getUserActivityData(UserHotpotJumpData.class, config.getActivityId(), getUserId());
        userGameData = new UserCommonActivityDataDaoImpl<UserHotpotJumpGameData>().getUserActivityData(UserHotpotJumpGameData.class, config.getActivityId(), getUserId());
        return false;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        if (userData != null) {
            if (userData.isInsertOption()) {
                new UserCommonActivityDataDaoImpl<UserHotpotJumpData>().add(userData);
            } else if (userData.isUpdateOption()) {
                new UserCommonActivityDataDaoImpl<UserHotpotJumpData>().update(userData);
            }
        }
        if (userGameData != null) {
            if (userGameData.isInsertOption()) {
                new UserCommonActivityDataDaoImpl<UserHotpotJumpGameData>().add(userGameData);
            } else if (userGameData.isUpdateOption()) {
                new UserCommonActivityDataDaoImpl<UserHotpotJumpGameData>().update(userGameData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        initSystemOpen();
    }

    public void initSystemOpen() {
        if (!isSystemOpen()) {
            return;
        }
        initUserData();
        syncConfig();
        syncUserData();
        resetOneDay(true);
    }

    public boolean isSystemOpen() {
        return SystemOpenMgr.systemOpen(player, eSystemId.HotpotJump.getValue());
    }

    public void initUserData() {
        if (!isSystemOpen()) {
            return;
        }
        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        if (config == null || HotpotJumpMgr.checkInShowTime(config.getActivityId()) != 0) {
            return;
        }
        synchronized (this) {
            if (userData == null || userData.getActivityId() != config.getActivityId()) {
                userData = new UserHotpotJumpData(config.getActivityId(), getUserId());
            }

            if (userGameData == null || userGameData.getActivityId() != config.getActivityId()) {
                userGameData = new UserHotpotJumpGameData(config.getActivityId(), getUserId());
            }
        }
    }

    private void syncConfig() {
        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return;
        }

        if (userData == null) {
            return;
        }
        HotpotJumpProto.HotpotJumpConfigSyncMsg.Builder configMsg = HotpotJumpMgr.getConfigMsg(player.getLanguage());
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_CONFIG_SYNC % 20000, configMsg);
    }

    private void syncUserData() {
        if (userData == null || userGameData == null) {
            return;
        }

        if (HotpotJumpMgr.checkInShowTime(userData.getActivityId()) != 0) {
            return;
        }

        HotpotJumpProto.HotpotJumpUserDataSyncMsg.Builder userDataMsg = HotpotJumpPb.parseHotpotJumpUserDataSyncMsg(userData, userGameData);
        if (practiceGameData != null) {
            userDataMsg.setIsPractice(true);
        }
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_USER_DATA_SYNC % 20000, userDataMsg);
    }

    public void resetOneDay(boolean isLogin) {
        if (userData == null) {
            return;
        }

        long currentTime = DateHelper.getCurrentTime();
        if (DateHelper.isSameDay(userData.getLastGameTime() / 1000, currentTime / 1000)) {
            return;
        }
        userData.setLastGameTime(currentTime);
        userData.setGameTimes(0);
        if (!isLogin) {
            syncUserData();
        }
    }

    public int checkInTime(int activityId) {
        int ret = HotpotJumpMgr.checkInTime(activityId);
        if (ret != 0) {
            return ret;
        }
        if (userData == null || userData.getActivityId() != activityId) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }
        return 0;
    }

    public int checkInShowTime(int activityId) {
        int ret = HotpotJumpMgr.checkInShowTime(activityId);
        if (ret != 0) {
            return ret;
        }
        if (userData == null || userData.getActivityId() != activityId) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }
        return 0;
    }

    public int startGame(int activityId, boolean isPractice) {
        int ret = checkInTime(activityId);
        if (ret != 0) {
            return ret;
        }

        HotpotJumpConfig config = HotpotJumpMgr.getConfig();

        String cost = "";
        if (!isPractice) {
            if (userGameData.getRound() == 0) {//创建新游戏
                if (userData.getGameTimes() >= config.getDayNum()) {
                    if (!player.getModule(CurrencyModule.class).removeCurrency(config.getEnergyCost(), eLogMoneyType.HotpotJump, eLogMoneyType.HotpotJumpStartGameCost)) {
                        return GameErrorCode.E_HOTPOT_JUMP_DAY_TIMES_LIMIT;
                    }
                    cost = config.getEnergyCost().toHelperString();
                } else {
                    userData.setGameTimes(userData.getGameTimes() + 1);
                }
                HotpotJumpGameLogic gameLogic = new HotpotJumpGameLogic(config, null);
                UserHotpotJumpGameData game = gameLogic.initGame();
                userGameData.copy(game);

                userData.setTotalGameTimes(userData.getTotalGameTimes() + 1);

                AutoLogMgr.add(new LogHotpotJumpStart(activityId, getUserId(), userData.getTotalGameTimes(), cost));
            }
        } else {
            if (practiceGameData == null) {
                HotpotJumpGameLogic gameLogic = new HotpotJumpGameLogic(config, null);
                UserHotpotJumpGameData game = gameLogic.initGame();
                game.setPractice(true);
                practiceGameData = game;
            }
        }

        HotpotJumpProto.HotpotJumpStartGameRespMsg.Builder resp = HotpotJumpProto.HotpotJumpStartGameRespMsg.newBuilder();
        resp.setRet(0);
        if (!isPractice) {
            resp.setGame(HotpotJumpPb.parseGameDataBuilder(userGameData));
        } else {
            resp.setGame(HotpotJumpPb.parseGameDataBuilder(practiceGameData));
        }

        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_START_GAME % 20000, resp);

        syncUserData();
        return 0;
    }

    public int giveUpGame(int activityId, boolean isPractice) {
        int ret = checkInShowTime(activityId);
        if (ret != 0) {
            return ret;
        }

        UserHotpotJumpGameData checkGameData = null;
        if (isPractice) {
            checkGameData = practiceGameData;
        } else {
            checkGameData = userGameData;
        }

        if (checkGameData == null || checkGameData.getRound() == 0) {
            return GameErrorCode.E_HOTPOT_JUMP_NO_GAME;
        }

        HotpotJumpProto.HotpotJumpGiveUpGameRespMsg.Builder resp = HotpotJumpProto.HotpotJumpGiveUpGameRespMsg.newBuilder();
        resp.setRet(0);
        resp.setScore(checkGameData.getScore());
        resp.setReward(gameOverSettle(true, checkGameData));
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_GIVE_UP_GAME % 20000, resp);

        return 0;
    }


    public int jump(HotpotJumpProto.HotpotJumpGetJumpReqMsg reqMsg) {
        int ret = checkInTime(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        if (reqMsg.getGridsCount() == 0) {
            return GameErrorCode.E_ERROR_PARAMETER;
        }

        HotpotJumpConfig config = HotpotJumpMgr.getConfig();

        UserHotpotJumpGameData checkGameData = null;
        if (reqMsg.getIsPractice()) {
            checkGameData = practiceGameData;
        } else {
            checkGameData = userGameData;
        }

        if (checkGameData == null || checkGameData.getRound() == 0) {
            return GameErrorCode.E_HOTPOT_JUMP_NO_GAME;
        }

        if (!checkGameData.getSkillIds().isEmpty()) {
            return GameErrorCode.E_HOTPOT_JUMP_SKILLS_NOT_EMPTY;
        }

        if (checkGameData.getMainRole().getGrid().getHp() <= 0) {
            return GameErrorCode.E_HOTPOT_JUMP_WAIT_REVIVE;
        }

        HotpotJumpGameLogic gameLogic = new HotpotJumpGameLogic(config, checkGameData);
        if (gameLogic.jump(reqMsg.getGridsList(), userData.getBuySilver() == 1, userData.getBuyGold() == 1) != 0) {
            return GameErrorCode.E_HOTPOT_JUMP_FAILURE;
        }

        checkGameData.copy(gameLogic.getGameData());

        boolean isEnd = checkGameOver(checkGameData);

        HotpotJumpProto.HotpotJumpGetJumpRespMsg.Builder resp = HotpotJumpProto.HotpotJumpGetJumpRespMsg.newBuilder();
        resp.setRet(0);
        resp.setGame(HotpotJumpPb.parseGameDataBuilder(checkGameData));
        resp.addAllShows(HotpotJumpPb.parseShowListBuilder(gameLogic.getShowList()));
        resp.setIsOver(isEnd);

        if (isEnd) {
            resp.setReward(gameOverSettle(false, checkGameData));
        } else {
            if (!checkGameData.isPractice()) {
                checkRankScoreChange();
            }
        }
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_GET_JUMP % 20000, resp);

        if (!reqMsg.getIsPractice()) {
            AutoLogMgr.add(gameLogic.parseMoveLog(getUserId(), userData.getTotalGameTimes()));
        }
        return 0;
    }

    public boolean checkGameOver(UserHotpotJumpGameData checkGameData) {
        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        boolean isEnd = false;
        if (!checkGameData.isPractice()) {
            if (checkGameData.getMainRole().getGrid().getHp() <= 0) {//主角阵亡
                if (checkGameData.getReviveTimes() >= config.getReviveLimit()) {//没有复活次数了,结算
                    isEnd = true;
                }
            }
            if (checkGameData.getRound() >= config.getMaxRound()) {
                isEnd = true;
            }
        } else {
            if (checkGameData.getMainRole().getGrid().getHp() <= 0) {//主角阵亡
                isEnd = true;
            } else if (checkGameData.getRound() >= config.getPracticeMaxRound()) {
                isEnd = true;
            }
        }

        return isEnd;
    }

    public void checkRankScoreChange() {
        if (userData == null) {
            return;
        }
        if (userGameData.getScore() > userData.getScore()) {
            userData.setScore(userGameData.getScore());
            userData.setMaxRound(userGameData.getRound());
            player.getModule(ActivityFundModule.class).updateProgress(userData.getActivityId(), userData.getScore());
        } else if (userGameData.getScore() == userData.getScore() && userGameData.getRound() < userData.getMaxRound()) {
            userData.setMaxRound(userGameData.getRound());
        } else {
            return;
        }
        player.notifyListener(eGamePlayerEventType.HotpotJumpScoreRank.getValue());
    }

    public String gameOverSettle(boolean isGiveUp, UserHotpotJumpGameData checkGameData) {
        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        String reward = "";
        if (!checkGameData.isPractice()) {//练习模式无奖励
            int endRound = checkGameData.getRound();
            checkRankScoreChange();

            HotpotJumpEvaluateConfig evaluateConfig = config.calcEvaluate(userGameData.getScore());
            if (evaluateConfig != null) {
                player.getModule(CurrencyModule.class).addCurrency(evaluateConfig.getReward(), eLogMoneyType.HotpotJump, eLogMoneyType.HotpotJumpEndReward);
                reward = evaluateConfig.getReward();
            }
            checkGameData.setRound(0);

            List<String> roleSkill = new ArrayList<>();
            for (List<Integer> skill : checkGameData.getMainRole().getSkills()) {
                String skillStr = skill.get(0) + "=" + skill.get(1);
                roleSkill.add(skillStr);
            }
            AutoLogMgr.add(new LogHotpotJumpGameOver(config.getActivityId(), getUserId(), userData.getTotalGameTimes(),
                    checkGameData.getMainRole().getLv(), roleSkill, checkGameData.getScore(), endRound, reward, isGiveUp));
        } else {
            practiceGameData = null;
        }

        syncUserData();

        return reward;
    }

    public int skillChoose(HotpotJumpProto.HotpotJumpLvUpSkillChooseReqMsg reqMsg) {
        int ret = checkInTime(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }
        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        UserHotpotJumpGameData checkGameData = null;
        if (reqMsg.getIsPractice()) {
            checkGameData = practiceGameData;
        } else {
            checkGameData = userGameData;
        }

        if (checkGameData == null || checkGameData.getRound() == 0) {
            return GameErrorCode.E_HOTPOT_JUMP_NO_GAME;
        }
        if (checkGameData.getSkillIds().isEmpty()) {
            return GameErrorCode.E_HOTPOT_JUMP_SKILLS_EMPTY;
        }

        if (!checkGameData.getSkillIds().contains(reqMsg.getSkillId())) {
            return GameErrorCode.E_HOTPOT_JUMP_SKILL_NOT_FOUND;
        }
        List<Integer> waitChoose = checkGameData.getSkillIds();

        HotpotJumpGameLogic gameLogic = new HotpotJumpGameLogic(config, checkGameData);
        gameLogic.skillChoose(reqMsg.getSkillId(), userData.getBuySilver() == 1, userData.getBuyGold() == 1);

        checkGameData.copy(gameLogic.getGameData());

        boolean isEnd = checkGameOver(checkGameData);

        HotpotJumpProto.HotpotJumpLvUpSkillChooseRespMsg.Builder resp = HotpotJumpProto.HotpotJumpLvUpSkillChooseRespMsg.newBuilder();
        resp.setRet(0);
        resp.setGame(HotpotJumpPb.parseGameDataBuilder(checkGameData));
        resp.addAllShows(HotpotJumpPb.parseShowListBuilder(gameLogic.getShowList()));
        resp.setIsOver(isEnd);
        if (isEnd) {
            resp.setReward(gameOverSettle(false, checkGameData));
        } else {
            if (!checkGameData.isPractice()) {
                checkRankScoreChange();
            }
        }
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_LV_UP_SKILL_CHOOSE % 20000, resp);

        if (!reqMsg.getIsPractice()) {
            AutoLogMgr.add(new LogHotpotJumpChoose(config.getActivityId(), getUserId(), userData.getTotalGameTimes(), checkGameData.getMainRole().getLv(), waitChoose, reqMsg.getSkillId()));
        }
        return 0;
    }

    public int skillRefresh(int activityId, boolean isPractice) {
        int ret = checkInTime(activityId);
        if (ret != 0) {
            return ret;
        }
        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        UserHotpotJumpGameData checkGameData = null;
        if (isPractice) {
            checkGameData = practiceGameData;
        } else {
            checkGameData = userGameData;
        }

        if (checkGameData == null || checkGameData.getRound() == 0) {
            return GameErrorCode.E_HOTPOT_JUMP_NO_GAME;
        }

        if (checkGameData.getSkillIds().isEmpty()) {
            return GameErrorCode.E_HOTPOT_JUMP_SKILLS_EMPTY;
        }

        if (checkGameData.getMainRole().getGrid().getHp() <= 0) {
            return GameErrorCode.E_HOTPOT_JUMP_WAIT_REVIVE;
        }

        String costStr = "";
        if (!checkGameData.isPractice()) {
            int refreshItemId = config.getRefreshItemId();
            Property cost = new Property(refreshItemId, 1);
            if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.HotpotJump, eLogMoneyType.HotpotJumpSkillRefreshCost)) {
                //道具不足判断元宝刷新次数
                List<Property> refreshCostList = config.getRefreshCostList();
                if (checkGameData.getRefreshTimes() >= refreshCostList.size()) {
                    return GameErrorCode.E_HOTPOT_JUMP_SKILL_REFRESH_TIMES_LIMIT;
                }
                cost = refreshCostList.get(checkGameData.getRefreshTimes());
                if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.HotpotJump, eLogMoneyType.HotpotJumpSkillRefreshCost)) {
                    return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                }
                checkGameData.setRefreshTimes(checkGameData.getRefreshTimes() + 1);
            }
            costStr = cost.toHelperString();
        }

        List<Integer> beforeSkills = checkGameData.getSkillIds();
        List<Integer> newSkills = config.randomMainRoleSkill(checkGameData.getMainRole().getSkills(), checkGameData.isPractice(), checkGameData.getMainRole().calcRemainHpRate());
        checkGameData.setSkillIds(newSkills);

        HotpotJumpProto.HotpotJumpLvUpSkillRefreshRespMsg.Builder resp = HotpotJumpProto.HotpotJumpLvUpSkillRefreshRespMsg.newBuilder();
        resp.setRet(0);
        resp.setGame(HotpotJumpPb.parseGameDataBuilder(checkGameData));
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_LV_UP_SKILL_REFRESH % 20000, resp);

        if (!isPractice) {
            AutoLogMgr.add(new LogHotpotJumpRefresh(config.getActivityId(), getUserId(), userData.getTotalGameTimes(), beforeSkills, newSkills, costStr));
        }

        return 0;
    }

    public int revive(int activityId, boolean isPractice) {
        int ret = checkInTime(activityId);
        if (ret != 0) {
            return ret;
        }
        UserHotpotJumpGameData checkGameData = null;
        if (isPractice) {
            checkGameData = practiceGameData;
        } else {
            checkGameData = userGameData;
        }

        if (checkGameData == null || checkGameData.getRound() == 0) {
            return GameErrorCode.E_HOTPOT_JUMP_NO_GAME;
        }

        if (checkGameData.getMainRole().getGrid().getHp() > 0) {
            return GameErrorCode.E_HOTPOT_JUMP_REVIVE_ERR;
        }

        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        if (checkGameData.getReviveTimes() >= config.getReviveLimit()) {
            return GameErrorCode.E_HOTPOT_JUMP_REVIVE_TIMES_LIMIT;
        }

        if (!isPractice) {
            if (!player.getModule(CurrencyModule.class).removeCurrency(config.getReviveConsume(), eLogMoneyType.HotpotJump, eLogMoneyType.HotpotJumpReviveCost)) {
                return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            }
        }

        HotpotJumpGameLogic gameLogic = new HotpotJumpGameLogic(config, checkGameData);
        gameLogic.revive(userData.getBuySilver() == 1, userData.getBuyGold() == 1);

        checkGameData.copy(gameLogic.getGameData());

        boolean isEnd = checkGameOver(checkGameData);

        HotpotJumpProto.HotpotJumpReviveRespMsg.Builder resp = HotpotJumpProto.HotpotJumpReviveRespMsg.newBuilder();
        resp.setRet(0);
        resp.setGame(HotpotJumpPb.parseGameDataBuilder(checkGameData));
        resp.addAllShows(HotpotJumpPb.parseShowListBuilder(gameLogic.getShowList()));
        resp.setIsOver(isEnd);
        if (isEnd) {
            resp.setReward(gameOverSettle(false, checkGameData));
        } else {
            if (!checkGameData.isPractice()) {
                checkRankScoreChange();
            }
        }
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_REVIVE % 20000, resp);
        return 0;
    }

    public UserHotpotJumpData getUserData() {
        return userData;
    }

    public UserHotpotJumpGameData getUserGameData() {
        return userGameData;
    }

    public int resetRoleGrid(HotpotJumpProto.HotpotJumpResetRoleGridReqMsg reqMsg) {
        int ret = checkInTime(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        UserHotpotJumpGameData checkGameData = null;
        if (reqMsg.getIsPractice()) {
            checkGameData = practiceGameData;
        } else {
            checkGameData = userGameData;
        }

        if (checkGameData == null || checkGameData.getRound() == 0) {
            return GameErrorCode.E_HOTPOT_JUMP_NO_GAME;
        }

        if (checkGameData.getMainRole().getGrid().getHp() <= 0) {
            return GameErrorCode.E_HOTPOT_JUMP_WAIT_REVIVE;
        }

        if (!checkGameData.getSkillIds().isEmpty()) {
            return GameErrorCode.E_HOTPOT_JUMP_SKILLS_NOT_EMPTY;
        }

        String beforePos = checkGameData.getMainRole().getGrid().getX() + "," + checkGameData.getMainRole().getGrid().getY();
        HotpotJumpGameLogic gameLogic = new HotpotJumpGameLogic(config, checkGameData);
        if (gameLogic.resetRoleGrid(reqMsg.getGrid()) != 0) {
            return GameErrorCode.E_HOTPOT_JUMP_RESET_ROLE_GRID_FAIL;
        }

        checkGameData.copy(gameLogic.getGameData());

        String afterPos = checkGameData.getMainRole().getGrid().getX() + "," + checkGameData.getMainRole().getGrid().getY();

        HotpotJumpProto.HotpotJumpResetRoleGridRespMsg.Builder resp = HotpotJumpProto.HotpotJumpResetRoleGridRespMsg.newBuilder();
        resp.setRet(0);
        resp.setGame(HotpotJumpPb.parseGameDataBuilder(checkGameData));
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_RESET_ROLE_GRID % 20000, resp);

        if (!checkGameData.isPractice()) {
            AutoLogMgr.add(new LogHotpotJumpReGrid(config.getActivityId(), getUserId(), userData.getTotalGameTimes(), beforePos, afterPos));
        }
        return 0;
    }

    public void buyToken(int shopType) {//处理游戏特权相关
        if (shopType == eShopType.ChineseChessSilverToken.getValue()) {//白银战令 连击所达成跳跃数-1 解锁局内人偶形象，
            userData.setBuySilver(1);

        } else if (shopType == eShopType.ChineseChessGoldToken.getValue()) {//黄金战令 火锅积分+10%
            userData.setBuyGold(1);
            if (userGameData.getRound() > 0) {
                HotpotJumpConfig config = HotpotJumpMgr.getConfig();
                int roundScore = config.getHOTPOTJUMP_ROUND_END_ADD_SCORE() * (userGameData.getRound() - 1);
                int gameScore = userGameData.getScore() - roundScore;
                BigDecimal newGameScore = BigDecimal.valueOf(gameScore).multiply(BigDecimal.valueOf(config.getTokenScoreMulti() + 1000).divide(BigDecimal.valueOf(1000)))
                        .setScale(0, RoundingMode.UP);
                userGameData.setScore(newGameScore.intValue() + roundScore);
                syncUserData();
            }
        }
    }

    public int skipGuide(HotpotJumpProto.HotpotJumpSkipGuideReqMsg reqMsg) {
        int ret = checkInTime(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        userData.setGuide("1");
        syncUserData();

        HotpotJumpProto.HotpotJumpSkipGuideRespMsg.Builder resp = HotpotJumpProto.HotpotJumpSkipGuideRespMsg.newBuilder();
        resp.setRet(0);
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_SKIP_GUIDE % 20000, resp);

        HotpotJumpConfig config = HotpotJumpMgr.getConfig();
        player.getModule(UserActModule.class).addCompleteGuideActivityType(config.getActivityInfo().getType(), config.getActivityInfo().getChildType());
        return 0;
    }

    public int getGuideMsg(HotpotJumpProto.HotpotJumpGetGuideMsgReqMsg reqMsg) {
        int ret = checkInTime(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        HotpotJumpProto.HotpotJumpGetGuideMsgRespMsg.Builder resp = HotpotJumpProto.HotpotJumpGetGuideMsgRespMsg.newBuilder();
        resp.setRet(0);
        resp.addAllGuide(HotpotJumpMgr.getGuideMsgList());
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_GET_GUIDE_MSG % 20000, resp);

        return 0;
    }

    public int checkEnemyAttack(HotpotJumpProto.HotpotJumpCheckEnemyAttackReqMsg reqMsg) {
        int ret = checkInTime(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        if (reqMsg.getGridsCount() == 0) {
            return GameErrorCode.E_ERROR_PARAMETER;
        }

        HotpotJumpConfig config = HotpotJumpMgr.getConfig();

        UserHotpotJumpGameData checkGameData = null;
        if (reqMsg.getIsPractice()) {
            checkGameData = practiceGameData;
        } else {
            checkGameData = userGameData;
        }

        if (checkGameData == null || checkGameData.getRound() == 0) {
            return GameErrorCode.E_HOTPOT_JUMP_NO_GAME;
        }

        if (!checkGameData.getSkillIds().isEmpty()) {
            return GameErrorCode.E_HOTPOT_JUMP_SKILLS_NOT_EMPTY;
        }

        if (checkGameData.getMainRole().getGrid().getHp() <= 0) {
            return GameErrorCode.E_HOTPOT_JUMP_WAIT_REVIVE;
        }

        HotpotJumpGameLogic gameLogic = new HotpotJumpGameLogic(config, checkGameData);
        if (gameLogic.jumpByOnlyCheckUnitAtk(reqMsg.getGridsList()) != 0) {
            return GameErrorCode.E_HOTPOT_JUMP_FAILURE;
        }


        HotpotJumpProto.HotpotJumpCheckEnemyAttackRespMsg.Builder resp = HotpotJumpProto.HotpotJumpCheckEnemyAttackRespMsg.newBuilder();
        resp.setRet(0);
        resp.addAllUnits(gameLogic.parseCanAtkUnitTempList());
        player.sendPacket(GameProtocol.S_HOTPOT_JUMP_CHECK_ENEMY_ATTACK % 20000, resp);

        return 0;
    }
}
